// eventBus.js
import { computed, reactive, readonly } from 'vue';

// 初始化状态，尝试从本地存储中加载数据
const initialState = {
    scores: JSON.parse(localStorage.getItem('scores')) || {},//学生成绩分布
    averageScores: JSON.parse(localStorage.getItem('averageScores')) || [],//平均分柱状图
    Top_variance: JSON.parse(localStorage.getItem('Top_variance')) || [],//方差前十
    DataToSubmit: JSON.parse(localStorage.getItem('DataToSubmit')) || [],
    studentCount: JSON.parse(localStorage.getItem('studentCount')) || [],
    computedDataForExtraTable: JSON.parse(localStorage.getItem('computedDataForExtraTable')) || [],
};

const state = reactive(initialState);

const methods = {
    setScores(scores) {
        state.scores = scores;
        localStorage.setItem('scores', JSON.stringify(scores));
    },
    setExtraTable(scores) {
        state.computedDataForExtraTable = scores;
        localStorage.setItem('computedDataForExtraTable', JSON.stringify(scores));
    },
    setDataToSubmit(scores) {
        state.DataToSubmit = scores;
        localStorage.setItem('DataToSubmit', JSON.stringify(scores));
    },
    setstudentCount(scores) {
        state.studentCount = scores;
        localStorage.setItem('studentCount', JSON.stringify(scores));
    },
    setTop_variance(scores) {
        state.Top_variance = scores;
        localStorage.setItem('Top_variance', JSON.stringify(scores));
    },
    setAverageScores(averageScores) {
        state.averageScores = averageScores;
        localStorage.setItem('averageScores', JSON.stringify(averageScores));
    },
    clearLocalStorage() {
        // 清空本地存储中的数据
        localStorage.removeItem('scores');
        localStorage.removeItem('averageScores');
        localStorage.removeItem('Top_variance');
        localStorage.removeItem('DataToSubmit');
        localStorage.removeItem('studentCount');
        localStorage.removeItem('computedDataForExtraTable');
        // 重置状态为初始状态
        Object.assign(state, initialState);
    }
};

export default { state: readonly(state), methods };
